package com.rio.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.SearchManager.OnCancelListener;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * 
 * TAB功能
 * 
 * @author rio
 * 
 */
public class TabHelper<T extends View> implements OnClickListener {

	private List<T> mViewMap;
	private int mSize;
	private int mCurrent;
	private OnTabChangeListener<T> mListener;

	private TabHelper() {

	}

	public TabHelper(OnTabChangeListener<T> listener) {
		mListener = listener;
	}

	public TabHelper(View parent, int[] radios) {
		addTabs(parent, radios);
	}

	public TabHelper(View parent, int[] radios, OnTabChangeListener<T> listener) {
		addTabs(parent, radios);
		mListener = listener;
	}

	public static <T extends View> TabHelper<T> bind(View parent, int checked,
			int[] radios, OnTabChangeListener<T> listener) {
		TabHelper<T> tab = new TabHelper<T>(parent, radios, listener);
		tab.setCheckedItem(checked, true);
		return tab;
	}

	public static <T extends View> TabHelper<T> create(
			OnTabChangeListener<T> listener) {
		return new TabHelper<T>(listener);
	}

	public void addTabs(View parent, int[] radios) {
		if (radios != null) {
			mSize = radios.length;
			for (int i = 0; i < mSize; i++) {
				add(parent.findViewById(radios[i]));
			}
		}
	}

	public List<T> getMap() {
		if (mViewMap == null) {
			mViewMap = new ArrayList<T>();
		}
		return mViewMap;
	}

	public void addTab(View view) {
		if (view != null) {
			view.setOnClickListener(this);
			T t = (T) view;
			int size = getMap().size();
			view.setId(size);
			getMap().add(t);
		}
	}
	
	private void add(View view) {
		if (view != null) {
			view.setOnClickListener(this);
			T t = (T) view;
			getMap().add(t);
		}
	}

	/**
	 * 修改序号
	 * 
	 * @param check
	 *            注意这是id号
	 */
	public boolean isCurrent(int check) {
		return mCurrent == check;
	}

	/**
	 * 修改序号
	 * 
	 * @param check
	 *            注意这是id号
	 */
	public void setCheckedItem(int check, boolean fromTouch) {
		if (mListener != null) {
			mCurrent = check;
			int size = getMap().size();
			T view = null;
			int id = 0;
			for (int i = 0; i < size; i++) {
				view = getMap().get(i);
				if (view != null) {
					id = view.getId();
					mListener.onChange(view, i, id, mCurrent == id, fromTouch);
				}

			}
		}

	}

	/**
	 * 修改序号
	 * 
	 * @param check
	 *            注意这是id号
	 */
	public void setCheckedPosition(int positon, boolean fromTouch) {
		if (mListener != null) {
			int size = getMap().size();
			T view = null;
			int id = 0;
			for (int i = 0; i < size; i++) {
				view = getMap().get(i);
				if (view != null) {
					boolean isCheck = positon == i;					
					id = view.getId();
					if(isCheck){
						mCurrent = id;
					}
					mListener.onChange(view, i, id, isCheck, fromTouch);
				}

			}
		}

	}
	
	
	public int getCurrent() {
		return mCurrent;
	}
	
	public T getItem(int position) {
		if (mViewMap != null && !mViewMap.isEmpty()) {
			return mViewMap.get(position);
		}
		return null;
	}

	public void setOnTabListener(OnTabChangeListener<T> listener) {
		mListener = listener;
	}

	@Override
	public void onClick(View v) {
		setCheckedItem(v.getId(), true);
	}

	public interface OnTabChangeListener<T> {

		/**
		 * 当发生OnClickListener，触发被选中的View的onCheck事件
		 * 
		 * @param view
		 *            当前选中的view
		 * @param position
		 * @param id
		 */
		public abstract void onChange(T view, int position, int id,
				boolean isCheck, boolean fromTouch);

	}

}
